package com.example.postmantest.controller;

import com.example.postmantest.model.Postman;
import com.example.postmantest.repository.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@CrossOrigin(origins = "http://127.0.0.1:5500/")
@RestController
@RequestMapping("/api")
public class PostmanController {

    @Autowired
    PostRepository postRepository;

    //查询所有
    @GetMapping("/t_article")
    public ResponseEntity<List<Postman>> getAllTutorial(@RequestParam(required = false) String title){
        try {
            List<Postman> postmens = new ArrayList<>();

            if (title == null)
                postRepository.findAll().forEach(postmens::add);
            else postRepository.findByTitleContaining(title).forEach(postmens::add);

            if (postmens.isEmpty()){
                return new ResponseEntity<>(HttpStatus.NO_CONTENT);
            }

            return new ResponseEntity<>(postmens,HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(null,HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    //根据id查询
    @GetMapping("/t_article/{id}")
    public ResponseEntity<Postman> getTutorialById(@PathVariable("id") int id) {
        Optional<Postman> postmanData = postRepository.findById(id);

        if (postmanData.isPresent()) {
            return new ResponseEntity<>(postmanData.get(),HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    //新增
    @PostMapping("/t_article")
    public ResponseEntity<Postman> createTutorial(@RequestBody Postman postman) {
        try {
            Postman addpost = postRepository
                    .save(new Postman(postman.getTitle()));
            return new ResponseEntity<>(addpost,HttpStatus.CREATED);
        } catch (Exception e) {
            return new ResponseEntity<>(null,HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    //更新指定id
    @PutMapping("/t_article/{id}")
    public ResponseEntity<Postman> updateTutroial(@PathVariable("id") int id,@RequestBody Postman postman) {
        Optional<Postman>  tutorialData = postRepository.findById(id);

        if (tutorialData.isPresent()) {
            Postman addpost = tutorialData.get();
            addpost.setTitle(postman.getTitle());
            return new ResponseEntity<>(postRepository.save(addpost),HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    //删除指定id
    @DeleteMapping("/t_article/{id}")
    public ResponseEntity<HttpStatus> deleteTutorial(@PathVariable("id") int id) {
        try {
            postRepository.deleteById(id);
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    //删除所有
    @DeleteMapping("/t_article")
    public ResponseEntity<HttpStatus> deleteAllTutorials() {
        try {
            postRepository.deleteAll();
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
